package day100.LC19;

public class Main {
    public static void main(String[] args) {

    }
}



//Definition for singly-linked list.
class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (n <= 0) {
            return head;
        }
        ListNode fast=head;
        ListNode slow=head;
        for (int i = 0; i <= n; i++) {
            if (fast != null) {
                fast=fast.next;
            }else {
                return head.next;
            }
        }
        while (fast != null) {
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return head;
    }
}
